#! /usr/bin/python
# encoding=utf-8
# Created by Fenglu Niu on 2025/5/11 20:34
import os

import requests
from dotenv import load_dotenv


class SchoolApi(object):
    __instance = None
    __is_first = True

    def __new__(cls):
        if not cls.__instance:
            cls.__instance = object.__new__(cls)
        return cls.__instance

    def __init__(self):
        if SchoolApi.__is_first:
            SchoolApi.__is_first = False
            load_dotenv()
            self.__api_base = os.getenv('SCHOOL_API_BASE')
            self.__stu_info_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_INFO')
            self.__stu_loan_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_LOAN')
            self.__stu_leave_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_LEAVE')
            self.__stu_book_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_BOOK')
            self.__stu_dormitory_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_DORMITORY')
            self.__stu_grade_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_GRADE')
            self.__stu_course_plan_url = self.__api_base + os.getenv('SCHOOL_API_GET_STU_COURSE_PLAN')
            self.__group_plan_url = self.__api_base + os.getenv('SCHOOL_API_GET_GROUP_PLAN')
            self.__jobs_url = self.__api_base + os.getenv('SCHOOL_API_GET_JOBS')

    def get_stu_info(self, stu_no: str):
        return requests.get(self.__stu_info_url, params={'xh': stu_no}).json()

    def get_stu_loan(self, stu_no: str):
        return requests.get(self.__stu_loan_url, params={'xh': stu_no}).json()

    def get_stu_leave(self, stu_no: str):
        return requests.get(self.__stu_leave_url, params={'xh': stu_no}).json()

    def get_stu_book(self, stu_no: str):
        return requests.get(self.__stu_book_url, params={'xh': stu_no}).json()

    def get_stu_dormitory(self, stu_no: str):
        return requests.get(self.__stu_dormitory_url, params={'xh': stu_no}).json()

    def get_stu_grade(self, stu_no: str, course_name: str):
        return requests.get(self.__stu_grade_url, params={'xh': stu_no, 'kcmc': course_name}).json()

    def get_stu_course_plan(self, stu_no: str, date: str):
        return requests.get(self.__stu_course_plan_url, params={'xh': stu_no, 'dateStr': date}).json()

    def get_group_plan(self, starttime: str):
        resp = requests.get(self.__group_plan_url, params={'starttime': starttime})
        return resp.json()

    def get_jobs(self, keyword: str):
        return requests.get(self.__jobs_url, params={'zwmc': keyword}).json()